package com.example.maruko_course_backend.services.impl;

import com.example.maruko_course_backend.services.KanjiTranslateApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import static com.example.maruko_course_backend.utils.StringUtil.COMMA;

@Service
public class KanjiTranslateApiServiceImpl implements KanjiTranslateApiService {

  @Value("${server.api.translate-api-url}")
  private String translateApiUrl;

  @Autowired
  private RestTemplate restTemplate;

  @Override
  public String callApiForTranslateKanji(String kanji) {
    String translateResults = callDirectlyToTranslate(kanji);
    return translateResults.substring(0, translateResults.indexOf(COMMA));
  }

  public String callDirectlyToTranslate(String kanji) {
    return restTemplate.getForObject(translateApiUrl + kanji, String.class);
  }
}
